package com.ruoyi.nuclein.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.basicdata.domain.Resident;
import com.ruoyi.basicdata.mapper.ResidentMapper;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.nuclein.Feign.BasicDataFeign;
import com.ruoyi.nuclein.domain.Residentmission;
import com.ruoyi.nuclein.mapper.ResidentmissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.nuclein.mapper.MissionMapper;
import com.ruoyi.nuclein.domain.Mission;
import com.ruoyi.nuclein.service.IMissionService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 核酸任务Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-04-08
 */
@Service
public class MissionServiceImpl implements IMissionService 
{
    @Resource
    private MissionMapper missionMapper;

    @Autowired
    private BasicDataFeign basicDataFeign;

    @Resource
    private ResidentmissionMapper residentmissionMapper;

    /**
     * 查询核酸任务
     * 
     * @param id 核酸任务主键
     * @return 核酸任务
     */
    @Override
    public Mission selectMissionById(Long id)
    {
        return missionMapper.selectMissionById(id);
    }

    /**
     * 查询核酸任务列表
     * 
     * @param mission 核酸任务
     * @return 核酸任务
     */
    @Override
    public List<Mission> selectMissionList(Mission mission){
        return missionMapper.selectMissionList(mission);
    }


    /**
     * 新增核酸任务
     * 
     * @param mission 核酸任务
     * @return 结果
     */
    @Override
    @Transactional
    public int insertMission(Mission mission){

        // 新增之前要修改楼栋状态
        Long buildingId = mission.getBuildingId();
        basicDataFeign.changeState(buildingId);
        // 设置楼栋任务状态，并添加
        changeState(mission);

        missionMapper.insertMission(mission);

        // 根据楼栋id 查询当前楼栋所有住户，添加到用户核酸表中
        List<Resident> residentList = basicDataFeign.getResidentByBuildingId(mission.getBuildingId());
        List<Long> ids = residentList.stream().map(Resident::getId).collect(Collectors.toList());

        // 将当前楼栋中所有住户的状态修改为待检测
        basicDataFeign.updateBatchStateByBuildingId(buildingId);

        // 添加住户核酸任务表
        Residentmission residentmission = new Residentmission();

        residentmission.setVillageId(mission.getVillageId());
        residentmission.setBuildingId(mission.getBuildingId());
        residentmission.setState(1L);
        residentmission.setStartTime(mission.getStartTime());
        residentmission.setEndTime(mission.getEndTime());
        residentmission.setCheckAddress(mission.getCheckAddress());
        residentmission.setNucleinMissionId(mission.getId());
        residentmissionMapper.insertBeatch(residentmission,ids);
        return 1;
    }

    /**
     * 修改核酸任务
     * 
     * @param mission 核酸任务
     * @return 结果
     */
    @Override
    public int updateMission(Mission mission){

        // 修改之前要修改楼栋状态
        Long buildingId = mission.getBuildingId();
        basicDataFeign.changeState(buildingId);

        // 设置任务状态
        changeState(mission);
        return missionMapper.updateMission(mission);
    }

    /**
     * 批量删除核酸任务
     * 
     * @param ids 需要删除的核酸任务主键
     * @return 结果
     */
    @Override
    public int deleteMissionByIds(Long[] ids){
        residentmissionMapper.deleteResidentmissionByMissionIds(ids);
        return missionMapper.deleteMissionByIds(ids);
    }

    /**
     * 删除核酸任务信息
     * 
     * @param id 核酸任务主键
     * @return 结果
     */
    @Override
    public int deleteMissionById(Long id)
    {
        return missionMapper.deleteMissionById(id);
    }

    @Override
    public String getMissNameByMissionId(Long id) {
        return missionMapper.getMissNameByMissionId(id);
    }

    /**
     *  获取所有结束时间小于当前时间的 核酸时间
     * @return
     */
    @Override
    public List<Long> selectMissionListByTime() {
        return missionMapper.selectMissionListByTime();
    }


    /**
     * 抽取出来的 添加和修改时的 修改任务人数和 修改任务状态方法
     * @param mission
     */
    private void changeState(Mission mission) {

        // 根据核酸任务中楼栋id查询楼栋总人数
        AjaxResult countByBuildId = basicDataFeign.getCountByBuildId(mission.getBuildingId());
        String peoles = String.valueOf(countByBuildId.get("count(*)"));
        mission.setMissionNum(peoles);
        // 设置核酸的完成人数，初始化为0
        mission.setCompleteNum("0");

        // 设置任务状态
        long currentTimeMillis = System.currentTimeMillis();
        long beginTime = mission.getStartTime().getTime();
        long endTime = mission.getEndTime().getTime();

        // 待检查
        if (currentTimeMillis - beginTime < 0) {
            mission.setState(1L);
        } else if (currentTimeMillis - beginTime >= 0 && currentTimeMillis - endTime <= 0) {
            // 进行中
            mission.setState(2L);
        } else if(currentTimeMillis - endTime > 0){
            // 已完成
            mission.setState(3L);
        }
    }
    
}
